
package com.csc440.team2.activity;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.csc440.team2.SmilConstants;
import com.csc440.team2.smil.SMILAudioComponent;
import com.csc440.team2.smil.SMILImageComponent;
import com.csc440.team2.smil.SMILMessage;
import com.csc440.team2.smil.SMILTextComponent;
import com.csc440.team2.smil.SMILVideoComponent;


/**
 * Selection activity allowing the user to select which type of component to add
 * to the message
 * 
 * @author Santiago Jamriska, Brad Gardner, Alex Povkov, Jeremy Spitzig
 * 
 */
public class AddComponents extends ListActivity
{
    private SMILMessage message;
    final static String[] MENUOPTIONS = new String[] {
            "TEXT COMPONENT", "IMAGE COMPONENT", "AUDIO COMPONENT", "VIDEO COMPONENT"
    };


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        Bundle bundle = getIntent().getExtras();

        if (bundle != null)
        {
            // retrieve the message we are working with from the bundle
            message = (SMILMessage) bundle.getSerializable(SmilConstants.MESSAGE);

            // retrieve the region that we are adding a component to
            final String regionId = bundle.getString(SmilConstants.REGION_ID);

            Button doneButton = new Button(this);
            doneButton.setText("Done");
            doneButton.setOnClickListener(new OnClickListener()
            {
                public void onClick(View v)
                {
                    // package message and return to the caller
                    Intent in = new Intent();
                    in.putExtra(SmilConstants.MESSAGE, message);
                    setResult(1, in);
                    finish();
                }

            });

            // set up the list view with component type options
            ListView lv = getListView();
            lv.addFooterView(doneButton);
            setListAdapter(new ArrayAdapter<String>(this, R.layout.second_menu, MENUOPTIONS));
            lv.setTextFilterEnabled(true);
            lv.setOnItemClickListener(new OnItemClickListener()
            {
                /*
                 * Forward processing to the add class for the selected
                 * component type
                 */
                public void onItemClick(AdapterView<?> parent, View clickedView, int position, long clickedItemId)
                {
                    if (position == SmilConstants.COMPONENT_TYPE_TEXT)
                    {
                        Intent in = new Intent(AddComponents.this, AddText.class);
                        in.putExtra(SmilConstants.REGION_ID, regionId);
                        AddComponents.this.startActivityForResult(in, 1);
                    }
                    else if (position == SmilConstants.COMPONENT_TYPE_AUDIO)
                    {
                        Intent in = new Intent(AddComponents.this, AddAudio.class);
                        in.putExtra(SmilConstants.REGION_ID, regionId);
                        AddComponents.this.startActivityForResult(in, 2);
                    }
                    else if (position == SmilConstants.COMPONENT_TYPE_IMAGE)
                    {
                        Intent in = new Intent(AddComponents.this, AddImage.class);
                        in.putExtra(SmilConstants.REGION_ID, regionId);
                        AddComponents.this.startActivityForResult(in, 3);
                    }
                    else if (position == SmilConstants.COMPONENT_TYPE_VIDEO)
                    {
                        Intent in = new Intent(AddComponents.this, AddVideo.class);
                        in.putExtra(SmilConstants.REGION_ID, regionId);
                        AddComponents.this.startActivityForResult(in, 4);
                    }
                }
            });
        }
    }


    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onSaveInstanceState()
     */
    @Override
    protected void onSaveInstanceState(Bundle bundle)
    {
        // save message to the bundle when this activity is left
        bundle.putSerializable(SmilConstants.MESSAGE, message);
    }


    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onActivityResult(int, int,
     * android.content.Intent)
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (data != null)
        {
            if (resultCode == SmilConstants.COMPONENT_TYPE_TEXT)
            {
                SMILTextComponent comp = (SMILTextComponent) data.getExtras().get(SmilConstants.COMPONENT);
                message.addComponent(comp);
            }
            else if (resultCode == SmilConstants.COMPONENT_TYPE_AUDIO)
            {
                SMILAudioComponent comp = (SMILAudioComponent) data.getExtras().get(SmilConstants.COMPONENT);
                message.addComponent(comp);
            }
            else if (resultCode == SmilConstants.COMPONENT_TYPE_IMAGE)
            {
                SMILImageComponent comp = (SMILImageComponent) data.getExtras().get(SmilConstants.COMPONENT);
                message.addComponent(comp);
            }
            else if (resultCode == SmilConstants.COMPONENT_TYPE_VIDEO)
            {
                SMILVideoComponent comp = (SMILVideoComponent) data.getExtras().get(SmilConstants.COMPONENT);
                message.addComponent(comp);
            }
        }
    }
}
